home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / Debug.java < prev    next >
Text File  |  1998-08-21  |  9KB  |  290 lines

  1. package symantec.itools.lang;
  2.  
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.IOException;
  6. import java.io.File;
  7. import java.io.FileWriter;
  8. import java.io.OutputStream;
  9. import java.io.OutputStreamWriter;
  10. import java.io.PrintWriter;
  11. import java.io.StringReader;
  12. import java.io.StringWriter;
  13. import java.io.Writer;
  14. import java.net.MalformedURLException;
  15. import java.net.URL;
  16. import java.text.DateFormat;
  17. import java.util.Calendar;
  18.  
  19.  
  20. /**
  21.  * Provides a single debug log file to be used by any number of
  22.  * classes.
  23.  *
  24.  * @author  D'Arcy Smith
  25.  * @version 1.0, 07/01/1998
  26.  * @since   VCafe 2.1a
  27.  */
  28.  
  29. public class Debug
  30. {
  31.     /**
  32.      * The PrintWriter used for output.
  33.      * @see #setWriter(java.lang.String, java.lang.Object, java.lang.String)
  34.      * @see #setWriter(java.net.URL, java.lang.Object, java.lang.String)
  35.      * @see #setWriter(java.io.File, java.lang.Object, java.lang.String)
  36.      * @see #setWriter(java.io.OutputStream, java.lang.Object, java.lang.String)
  37.      * @see #setWriter(java.io.Writer, java.lang.Object, java.lang.String)
  38.      */
  39.     protected static PrintWriter writer;
  40.     /**
  41.      * The value of the system property "os.name";
  42.      */
  43.     protected static String      OSName;
  44.     /**
  45.      * The value of the system property "os.version";
  46.      */
  47.     protected static String      OSVersion;
  48.     /**
  49.      * The value of the system property "os.arch";
  50.      */
  51.     protected static String      OSPlatform;
  52.  
  53.     static
  54.     {
  55.         OSName     = System.getProperty("os.name");
  56.         OSVersion  = System.getProperty("os.version");
  57.         OSPlatform = System.getProperty("os.arch");
  58.     }
  59.  
  60.     /**
  61.      * Constructor - this should not be called... it is only protected
  62.      * so that Debug can be subclassed.
  63.      */
  64.     protected Debug()
  65.     {
  66.     }
  67.  
  68.     /**
  69.      * Set the file name to write the debug log to
  70.      *
  71.      * @param     fileName the file name of the debug log
  72.      * @param     clazz    the class that had "main" called
  73.      * @param     version  the version of clazz
  74.      * @return    true  if the writer was set
  75.      *            false if the writer already has a value
  76.      * @exception IOException if any error occured attaching the log file
  77.      */
  78.     public static synchronized boolean setWriter(String fileName, Object clazz, String version)
  79.         throws IOException
  80.     {
  81.         return (setWriter(new File(fileName), clazz, version));
  82.     }
  83.  
  84.     /**
  85.      * Set the url to write the debug log to
  86.      *
  87.      * @param     url the url of the debug log
  88.      * @param     clazz    the class that had "main" called
  89.      * @param     version  the version of clazz
  90.      * @return    true  if the writer was set
  91.      *            false if the writer already has a value
  92.      * @exception IOException if any error occured attaching the log file
  93.      */
  94.     public static synchronized boolean setWriter(URL url, Object clazz, String version)
  95.         throws IOException
  96.     {
  97.         return (setWriter(url.getFile(), clazz, version));
  98.     }
  99.  
  100.     /**
  101.      * Set the file to write the debug log to
  102.      *
  103.      * @param     file the file of the debug log
  104.      * @param     clazz    the class that had "main" called
  105.      * @param     version  the version of clazz
  106.      * @return    true  if the writer was set
  107.      *            false if the writer already has a value
  108.      * @exception IOException if any error occured attaching the log file
  109.      */
  110.     public static synchronized boolean setWriter(File file, Object clazz, String version)
  111.         throws IOException
  112.     {
  113.         return (setWriter(new FileWriter(file), clazz, version));
  114.     }
  115.  
  116.     /**
  117.      * Set the stream to write the debug log to
  118.      *
  119.      * @param     stream the stream of the debug log
  120.      * @param     clazz    the class that had "main" called
  121.      * @param     version  the version of clazz
  122.      * @return    true  if the writer was set
  123.      *            false if the writer already has a value
  124.      * @exception IOException if any error occured attaching the log file
  125.      */
  126.     public static synchronized boolean setWriter(OutputStream stream, Object clazz, String version)
  127.     {
  128.         return (setWriter(new OutputStreamWriter(stream), clazz, version));
  129.     }
  130.  
  131.     /**
  132.      * Set the writer to write the debug log to.
  133.      * If the writer (of the class - not the param)
  134.      * is null then the log will be assigned.
  135.      *
  136.      * @param     writer the writer of the debug log
  137.      * @param     clazz    the class that had "main" called
  138.      * @param     version  the version of clazz
  139.      * @return    true  if the writer was set
  140.      *            false if the writer already has a value
  141.      * @exception IOException if any error occured attaching the log file
  142.      */
  143.     public static synchronized boolean setWriter(Writer w, Object clazz, String version)
  144.     {
  145.         if(writer == null)
  146.         {
  147.             writer = new PrintWriter(w, true);
  148.  
  149.             writer.println("OS Information:");
  150.             writer.println("   - OS          : " + OSName);
  151.             writer.println("   - Version     : " + OSVersion);
  152.             writer.println("   - Platform    : " + OSPlatform);
  153.             writer.println("   - JDK Version : " + System.getProperty("java.version"));
  154.             writer.println("   - JDK Vendor  : " + System.getProperty("java.vendor"));
  155.             writer.println();
  156.             writer.println("Program Information:");
  157.             writer.println("   - Main Class : " + clazz.getClass().getName());
  158.             writer.println("   - Version    : " + version);
  159.             writer.println();
  160.             writer.println("Start of debugging log - Time: " +
  161.                            DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(Calendar.getInstance().getTime()));
  162.             writer.println();
  163.  
  164.             return (true);
  165.         }
  166.  
  167.         return (false);
  168.     }
  169.  
  170.     /**
  171.      * Write out a debug message
  172.      *
  173.      * @param msg the message to write
  174.      */
  175.     public static synchronized void write(String msg)
  176.     {
  177.         if(writer != null)
  178.         {
  179.             writer.println(msg);
  180.         }
  181.     }
  182.  
  183.     /**
  184.      * Write out a debug message
  185.      *
  186.      * @param src an instance of the class where the message is coming from
  187.      * @param msg the message to write
  188.      */
  189.     public static synchronized void write(Object src, long line, String msg)
  190.     {
  191.         if(writer != null)
  192.         {
  193.             writer.println(src.getClass().getName() + " :: " + convertLineNumber(line) + " :: " + msg);
  194.         }
  195.     }
  196.  
  197.     /**
  198.      * Write out a debug message
  199.      *
  200.      * @param src    an instance of the class where the message is coming from
  201.      * @param action the action that is being reported
  202.      * @param result the reult of the action
  203.      */
  204.     public static synchronized void write(Object src, long line, String action, String result)
  205.     {
  206.         if(writer != null)
  207.         {
  208.             writer.println(src.getClass().getName() + " :: " + convertLineNumber(line) + " :: " + action + " :: " + result);
  209.         }
  210.     }
  211.  
  212.     /**
  213.      * Write out a debug message
  214.      *
  215.      * @param src an instance of the class where the message is coming from
  216.      * @param ex  the exception that was thrown
  217.      */
  218.     public static synchronized void write(Object src, long line, Throwable ex)
  219.     {
  220.         if(writer != null)
  221.         {
  222.             writer.println(src.getClass().getName() + " :: " + convertLineNumber(line) + " :: " + ex.getMessage());
  223.             ex.printStackTrace(writer);
  224.         }
  225.     }
  226.  
  227.     /**
  228.      * Finish writing the debug log.
  229.      */
  230.     public static synchronized void finish()
  231.     {
  232.         writer.println();
  233.         writer.println("End of debugging log - Time: " +
  234.                         DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(Calendar.getInstance().getTime()));
  235.         writer.close();
  236.     }
  237.  
  238.     /**
  239.      * Get the current line number
  240.      *
  241.      * This has only been tested with the Sun JDK 1.1.5 and VCafe JDK 1.1.4
  242.      * on Windows.  I need to make use of the OS class & Java Vendor
  243.      * to make it work for others (assuming they display stack traces
  244.      * differently from Sun and Symantec).
  245.      */
  246.     public static synchronized long getLineNumber()
  247.     {
  248.         Throwable      stackTrace;
  249.         StringWriter   writer;
  250.         BufferedReader reader;
  251.  
  252.         stackTrace = new Throwable().fillInStackTrace();
  253.         writer     = new StringWriter();
  254.         stackTrace.printStackTrace(new PrintWriter(writer));
  255.         reader     = new BufferedReader(new StringReader(writer.toString()));
  256.  
  257.         try
  258.         {
  259.             String line;
  260.  
  261.             reader.readLine();
  262.             reader.readLine();
  263.             line = reader.readLine();
  264.  
  265.             if(line != null)
  266.             {
  267.                 line = line.substring(line.lastIndexOf(':') + 1, line.length() - 1);
  268.                 return (Long.parseLong(line));
  269.             }
  270.         }
  271.         catch(IOException ex)
  272.         {
  273.         }
  274.         catch(NumberFormatException ex)
  275.         {
  276.         }
  277.  
  278.         return (-1L);
  279.     }
  280.  
  281.     private static String convertLineNumber(long line)
  282.     {
  283.         if(line == -1)
  284.         {
  285.             return ("Unknown line");
  286.         }
  287.  
  288.         return (Long.toString(line));
  289.     }
  290. }